<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\9\27 0027
 * Time: 13:42
 */

namespace app\Lib\Ioc\Systems;


use app\Lib\Exception\SysException;
use app\Lib\Model\Systems\RequestParam;
use app\Lib\Model\Systems\TopServer;
use think\Request;
use think\Session;

class TopClient
{
    public $error="TopClient";
    private $appkey;
    private $getData;
    private $postData;
    private $ip;
    private $requestTime;
    private $requestSignTime;
    private $request;
    private $requestParam;
    public $method;

    /**
     * 初始化
     * TopClient constructor.
     * @param Request $request
     * @param RequestParam $requestParam
     * @throws SysException
     */
    public function __construct(Request $request,RequestParam $requestParam)
    {
        $this->request = $request;
        $this->requestParam = $requestParam;
        $this->paramCreate();
    }

    /**
     * 参数整合
     * @throws SysException
     */
    protected function paramCreate(){
        $request = $this->request;
        if(!$request) throw new SysException("参数为空",$this->error);
        //提取get参数
        $getData = $request->get()?$request->get():[];
        //提取post参数
        $postData = $request->post()?$request->post():[];
        //提取请求ip
        $ip=$request->ip();
        //记录当前时间
        $requestTime = time();
        //提取请求时间
        $requestSignTime = isset($getData['timestamp'])?$getData['timestamp']:"";
        //从get参数提取appkey
        $appkey=isset($getData['appkey'])?$getData['appkey']:"";
        //获取请求接口名称
        $method= isset($getData['method'])?trim($getData['method']):"";
        $version= isset($getData['version'])?trim($getData['version']):"";
        Session::set('opIp',$ip);
        $this->requestParam->postData=$postData;
        $this->requestParam->getData=$getData;
        $this->requestParam->ip=$ip;
        $this->requestParam->requestTime=$requestTime;
        $this->requestParam->requestSignTime=$requestSignTime;
        $this->requestParam->appkey=$appkey;
        $this->requestParam->method=$method;
        $this->requestParam->version=$version;

    }

    /**
     * 参数验证
     * @throws SysException
     */
    public function check()
    {

        if (!$this->requestParam->method) throw new SysException("请求方式method不能为空");
        $top = new TopServer($this->requestParam->appkey);
        $top->sign=isset($this->requestParam->getData['sign'])?$this->requestParam->getData['sign']:"";
        $top->format=isset($this->requestParam->getData['format'])?$this->requestParam->getData['format']:"json";
        $top->method=isset($this->requestParam->getData['method'])?$this->requestParam->getData['method']:"";
        $top->requestSignTime=$this->requestParam->requestSignTime;
        $top->requestTime=$this->requestParam->requestTime;
        $top->version=$this->requestParam->version;
//        $top->ip = $this->requestParam->ip;
        $top->execute($this->requestParam->postData);
        $top->IpCheck($this->requestParam->ip);
        //$top->checkTime();

    }

    public function getParam($type="post"){
        if($type=="post")return $this->postData;
        return $this->getData;
    }
    public function __get($name)
    {
        return $this->$name;
    }
}